package org.lilred.nzbberry.controllers;

import org.lilred.nzbberry.http.ConnectionManager;

import net.rim.device.api.browser.field2.BrowserField;
import net.rim.device.api.browser.field2.BrowserFieldConfig;
import net.rim.device.api.browser.field2.BrowserFieldHistory;
import net.rim.device.api.browser.field2.BrowserFieldRequest;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class SearchController extends PageController {
	public MainScreen searchView = new MainScreen(MainScreen.HORIZONTAL_SCROLL | MainScreen.VERTICAL_SCROLL) {
		protected void makeMenu(Menu menu, int instance) {
			BrowserFieldHistory bfHistory = browserField.getHistory();
			menu.add(zoomInMenuItem);
			menu.add(zoomOutMenuItem);
			if (bfHistory.canGoBack()) menu.add(backMenuItem);
			if (bfHistory.canGoForward()) menu.add(forwardMenuItem);
			menu.add(refreshMenuItem);
			menu.add(closeMenuItem);
		}
	    protected void onUiEngineAttached(boolean attached)
	    {
	        if(attached)
	        {
	            try
	            {
	            	browserField.displayContent(html,"");
	            }
	            catch(Exception e)
	            {                
	                deleteAll();
	                add(new LabelField("ERROR:\n\n"));
	                add(new LabelField(e.getMessage()));
	            }
	        }
	    }		
	};
	BrowserField browserField = new BrowserField();
	BrowserFieldRequest lastRequest;
	public SearchController() {
		view = new VerticalFieldManager(VerticalFieldManager.HORIZONTAL_SCROLL | VerticalFieldManager.VERTICAL_SCROLL | VerticalFieldManager.USE_ALL_HEIGHT) {
			protected void paint(Graphics g) {
				g.setBackgroundColor(0xFFFFFF);
				g.clear();
				super.paint(g);
			}
		};
		BrowserController browserController = new BrowserController(browserField);
		ConnectionManager connectionManager = new ConnectionManager(browserField);
		//browserController.browserField.getConfig().setProperty(BrowserFieldConfig.CONTROLLER, browserController);
		browserField.getConfig().setProperty(BrowserFieldConfig.CONNECTION_MANAGER, connectionManager);
		searchView.add(browserField);
		browserField.setZoomScale(2);
	}
	
	String html = "<a href=\"http://www.nzbs.org\">NZBS.org</a><br/><a href=\"http://www.newzbin.com\">Newzbin</a>";
	
	MenuItem backMenuItem = new MenuItem("Back", 20, 20) {
		public void run() {
			browserField.back();
		}
	};
	
	MenuItem refreshMenuItem = new MenuItem("Refresh", 30, 30) {
		public void run() {
			browserField.refresh();
		}
	};
	
	MenuItem forwardMenuItem = new MenuItem("Forward", 40, 40) {
		public void run() {
			browserField.forward();
		}
	};

	MenuItem zoomInMenuItem = new MenuItem("Zoom In", 60, 60) {
		public void run() {
			zoomIn();
		}
	};
	
	protected void zoomIn() {
		float currentZoom = browserField.getZoomScale();
		browserField.setZoomScale(1.0F);
	}
	
	MenuItem zoomOutMenuItem = new MenuItem("Zoom Out", 70, 70) {
		public void run() {
			zoomOut();
		}
	};
	
	protected void zoomOut() {
		float currentZoom = browserField.getZoomScale();
		browserField.setZoomScale(0F);
	}
	
	MenuItem closeMenuItem = new MenuItem("Back to NZBberry", 80, 80) {
		public void run() {
			UiApplication.getUiApplication().popScreen(searchView);
		}
	};
	public Menu makeMenu(Menu menu) {
		return null;
	}
}
